﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ShopManagement.Dao;
using ShopManagement.Common;

namespace ShopManagement.Forms
{
    public partial class ProductForm : Form
    {
        #region Attributes

        private SmDao _myDao;
        private Product _myData;
        //0: new, 1:update
        private int _mode;

        #endregion

        #region Methods

        public ProductForm(SmDao inputDao)
        {
            InitializeComponent();
            _myDao = inputDao;
            _mode = 0;
            cbPdType.SelectedIndex = 0;
        }

        public ProductForm(SmDao inputDao, Product myData)
        {
            InitializeComponent();
            _mode = 1;
            _myDao = inputDao;
            _myData = myData;
            lbPdCode.Text = _myData.ProductCode;
            txtProductName.Text = _myData.ProductName;
            txtPrice.Text = _myData.Price.ToString();
            txtNetValue.Text = _myData.NetValue.ToString();
            txtQuantity.Text = _myData.Quantity.ToString();
            txtImage.Text = _myData.Image;
            txtDetail.Text = _myData.Detail;
            if (_myData.Type == "A")
            {
                cbPdType.SelectedIndex = 0;
            }
            else if (_myData.Type == "Q")
            {
                cbPdType.SelectedIndex = 1;
            }
            else if (_myData.Type == "G")
            {
                cbPdType.SelectedIndex = 2;
            }
            else
            {
                cbPdType.SelectedIndex = 3;
            }
        }

        private void ProductForm_Load(object sender, EventArgs e)
        {
        }

        private void btBrowser_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtImage.Text = openFileDialog1.FileName;
                try
                {
                    picBox1.ImageLocation = openFileDialog1.FileName;
                    picBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    picBox1.Load();
                }
                catch (Exception)
                {
                    MessageBox.Show("File Được Chọn Không Hợp Lê!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtImage.Text = "";
                }
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            int price;
            int netValue;
            int quantity;
            //0: ok, 1:wrong
            int status = 0;
            int selectedIndex = cbPdType.SelectedIndex;
            string type = "A";
            if (selectedIndex == 1)
            {
                type = "Q";
            }
            else if (selectedIndex == 2)
            {
                type = "G";
            }
            else if (selectedIndex == 3)
            {
                type = "P";
            }
            if (_myDao.ValidateProductCode(lbPdCode.Text) && _mode !=1)
            {
                status = 1;
                MessageBox.Show("Mã Sản Phẩm Đã Tồn Tại", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!int.TryParse(txtNetValue.Text, out netValue) || netValue < 1)
            {
                status = 1;
                MessageBox.Show("Giá Nhập Không Hợp Lệ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!int.TryParse(txtPrice.Text, out price) || price < 1)
            {
                status = 1;
                MessageBox.Show("Giá Bán Không Hợp Lệ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!int.TryParse(txtQuantity.Text, out quantity) || quantity < 1)
            {
                status = 1;
                MessageBox.Show("Số Lượng Không Hợp Lệ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (status != 1)
            {
                if (_mode == 0)
                {
                    Product temp = new Product();
                    temp.ProductCode = lbPdCode.Text;
                    temp.ProductName = txtProductName.Text;
                    temp.Price = int.Parse(txtPrice.Text);
                    temp.NetValue = int.Parse(txtNetValue.Text);
                    temp.Quantity = int.Parse(txtQuantity.Text);
                    temp.Detail = txtDetail.Text;
                    temp.Type = type;
                    if (!String.IsNullOrEmpty(txtImage.Text))
                    {
                        temp.Image = GetDestinationPath(txtImage.Text, temp);
                        CopyImage(txtImage.Text, temp);
                    }
                    else
                    {
                        temp.Image = "";
                    }
                    _myDao.InsertProduct(temp);
                    
                }
                else
                {
                    _myData.ProductCode = lbPdCode.Text;
                    _myData.ProductName = txtProductName.Text;
                    _myData.Price = int.Parse(txtPrice.Text);
                    _myData.NetValue = int.Parse(txtNetValue.Text);
                    _myData.Quantity = int.Parse(txtQuantity.Text);
                    _myData.Detail = txtDetail.Text;
                    _myData.Type = type;
                    if (!String.IsNullOrEmpty(txtImage.Text))
                    {
                        _myData.Image = GetDestinationPath(txtImage.Text, _myData);
                        CopyImage(txtImage.Text, _myData);
                    }
                    else
                    {
                        _myData.Image = "";
                    }
                    _myDao.UpdateProduct(_myData);
                }
                Close();
            }
        }

        private void CopyImage(string basePath, Product myPd)
        {
            string desPath = GetDestinationPath(basePath, myPd);
            if (Directory.Exists(Path.GetDirectoryName(desPath)))
            {
                File.Copy(basePath, desPath, true);
            }
            else
            {
                Directory.CreateDirectory(Path.GetDirectoryName(desPath));
                File.Copy(basePath, desPath, true);
            }
        }

        private string GetDestinationPath(string basePath, Product myPd)
        {
            string extension = Path.GetExtension(basePath);
            string desPath = Utility.GetPathToCurrentFolder();
            string name = myPd.ProductCode + "_" + (myPd.Price/1000) + extension;
            if (myPd.Type == "A")
            {
                desPath += Constants.AFolder + name;
            }
            else if (myPd.Type == "Q")
            {
                desPath += Constants.QFolder + name;
            }
            else if (myPd.Type == "G")
            {
                desPath += Constants.GFolder + name;
            }
            else
            {
                desPath += Constants.PFolder + name;
            }
            return desPath;
        }

        private void cbPdType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = cbPdType.SelectedIndex;
            string type = "A";
            if (selectedIndex == 1)
            {
                type = "Q";
            }
            else if (selectedIndex == 2)
            {
                type = "G";
            }
            else if (selectedIndex == 3)
            {
                type = "P";
            }
            if (_mode == 0)
            {
                lbPdCode.Text = _myDao.GetProductCode(type);
            }
            else
            {
                if(type == _myData.Type)
                {
                    int number = int.Parse(_myDao.GetProductCode(type).Substring(1));
                    number -= 1;
                    lbPdCode.Text = type + number;
                }
                else
                {
                    lbPdCode.Text = _myDao.GetProductCode(type);
                }
            }
        }
        #endregion
    }
}